$('#question').live("pagebeforeshow", function(event) {
	createQuestionPage();
});

function createQuestionPage() {
	question = JSON.parse(localStorage["question"]);
	
	$('#question_header').empty();
	$('#question_header').append(question.text);
	
	var answerList = $('#answer_list');
	answerList.empty();
	
	fetchAllAnswers();
	answerList.listview("refresh");
	
	function fetchAllAnswers() {
		$.ajax( {
			url: "/api/antwoord/"+question.id+"?rpp=1000",
			async: false,	// Now we wait till we get response.
			dataType:"json",
			success: function(data) { 
				var answers = data.result;
				for(var i=0; i < answers.length;i++) {
					li = $('<li><p style="white-space:normal"><strong>'+ answers[i].text + '</strong></p></li>');
					answerList.append(li);
				}				
			},
			error: function() {
				console.log("** ERROR: Can not load json object **");
			}			
		});
}


}

$('#answer_form').submit(function(){
	console.log("TEST");
});

function postNewAnswer() {
	var question = JSON.parse(localStorage["question"]);
	
	var a = { "vraagId":question.id, "text":question_answer.value};
	console.log(JSON.stringify(a));
	var a1 = JSON.stringify(a);

	$.ajax({ 
		url: '/api/antwoord/add',
		type: 'POST',
		data: a1,
		dataType:"json",
		contentType: 'application/json',
	});
	
	li = $('<li>'+ question_answer.value + '</li>')
	$('#answer_list').append(li);
	$('#answer_list').listview("refresh");
	question_answer.value = "";
}